/**
 * @function 创建人物及其属性
 * @encode utf-8
 * @copyright (C) lijun
 * @file People.js
 * @author lijun
 * @version 1.0
 * @date 2009-3-22
 */

/**
 * 构造函数
 * @param posId 人物位置id
 * @param name 名称
 * @param type 类型
 */
function People(posId, name, type)
{
    this.posId = posId;
    this.name = name;
    this.type = type;
    this.prefix = "RPG_";
    this.className = "base way";
    this.client = RIAClient;
    this.people = this.client.$E(this.prefix + this.name + "_" + this.type);
    this.walkTimer = null;
    this.walkFrame = 0;
    this.walkFrameRate = 72;
    this.walkDirection = 0;
    this.walkOffset = 10;
}

People.prototype.getInstance = function()
{
    var instance = null;
    if(null != this.people)
    {
        instance = this.people;
    }
    else
    {
        this.setStyle();
        instance = this.client.$C("div");
        instance.setAttribute("id", this.prefix + this.name + "_" + this.type);
        instance.className = this.className + "00";
        this.people = instance;
    }
    return instance;
};

People.prototype.setStyle = function()
{
    var styles = [
        'div.base{position:absolute; left:0px; top:0px; width:30px; height:46px; background:transparent url(images/people_opacity.gif) no-repeat 0px 0px;}',
        'div.way00{background-position:-3px 0px}',
        'div.way01{background-position:-34px -2px}',
        'div.way02{background-position:-65px -2px}',
        'div.way10{background-position:-2px -49px}',
        'div.way11{background-position:-34px -49px}',
        'div.way12{background-position:-64px -49px}',
        'div.way20{background-position:-4px -97px}',
        'div.way21{background-position:-34px -97px}',
        'div.way22{background-position:-65px -97px}',
        'div.way30{background-position:-3px -144px}',
        'div.way31{background-position:-34px -146px}',
        'div.way32{background-position:-65px -146px}'
    ];
    this.client.dynamicStyle(styles);
};

People.prototype.captureEvent = function()
{
    if(null == this.walkTimer)
    {
        var keyCode = window.event.keyCode;
        var _people = this;
        switch(keyCode)
        {
            case 37: //left
                this.walkTimer = setInterval(function(){_people.walk(1)}, this.walkFrameRate);
                break;
            case 38: //up
                this.walkTimer = setInterval(function(){_people.walk(3)}, this.walkFrameRate);
                break;
            case 39: //right
                this.walkTimer = setInterval(function(){_people.walk(2)}, this.walkFrameRate);
                break;
            case 40: //down
                this.walkTimer = setInterval(function(){_people.walk(0)}, this.walkFrameRate);
                break;
        }
    }
};

People.prototype.clearWalkTimer = function()
{
    if(null != this.walkTimer)
    {
        clearInterval(this.walkTimer);
        this.walkTimer = null;
    }
    this.walkFrame = 0;
    this.setWalkDirection();
};

People.prototype.setWalkDirection = function()
{
    this.people.className = this.className + this.walkDirection + this.walkFrame;
};

People.prototype.walk = function(dir)
{
    if(this.walkDirection != dir)
        this.clearWalkTimer();
    this.walkDirection = dir;
    this.setWalkDirection();
    this.walkFrame++;
    if(this.walkFrame > 2)
        this.walkFrame = 0;
    switch(this.walkDirection)
    {
        case 0:
            this.people.style.top = Math.min((parseInt(this.people.style.top||0, 10) + this.walkOffset), this.client.getViewportSize().height-46) + "px";
            break;
        case 1:
            this.people.style.left = Math.max((parseInt(this.people.style.left||0, 10) - this.walkOffset), 0) + "px";
            break;
        case 2:
            this.people.style.left = Math.min((parseInt(this.people.style.left||0, 10) + this.walkOffset), this.client.getViewportSize().width-30) + "px";
            break;
        case 3:
            this.people.style.top = Math.max((parseInt(this.people.style.top||0, 10) - this.walkOffset), 0) + "px";
            break;
    }
};

People.prototype.init = function()
{
    var position = this.client.$E(this.posId) || document.body;
    var instance = this.getInstance();
    var _people = this;
    this.client.$A(instance, position);
    document.attachEvent("onkeydown", function(){_people.captureEvent()});
    document.attachEvent("onkeyup", function(){_people.clearWalkTimer()});
};